package com.netty8018.nettydemo;

import ch.qos.logback.core.net.ssl.SSL;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author liyanhao
 * @Date 2023-02-04 17:51
 */
public class NettyServer {
    public static void main(String[] args) throws InterruptedException {
        //创建BossGroup和Workgroup
        //说明
        //1.创建两个线程组bossGroup和workgroup
        //2.bossGroup只是处理连接请求，真正的和客户端业务处理会交给workerGroup完成
        //3.两个都是无限循环
        //默认实际是cpu核数*2
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();
        try{
            //创建服务器端的启动对象 配置参数
            ServerBootstrap bootstrap = new ServerBootstrap();
            //使用链式编程来进行配置
            bootstrap.group(bossGroup,workerGroup)//设置两个线程组
                    .channel(NioServerSocketChannel.class)//使用NIOSocketChannel作为服务器的通道实现
                    .option(ChannelOption.SO_BACKLOG,128)//设置线程队列得到连接个数
                    .childOption(ChannelOption.SO_KEEPALIVE,true)//设置线程队列的连接个数
                    .childHandler(new ChannelInitializer<SocketChannel>() {//创建一个通道测试对象 匿名对象
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new NettyServerHeandler());
                        }
                    });//给我们的workerGroup的EventLoop对应的管道设置处理器
            System.out.println("...服务器 is ready ...");
            //绑定一个端口并且同步，生成一个channelFuture对象
            //启动服务器 并绑定端口
            ChannelFuture cf = bootstrap.bind(6668).sync();
            //对关闭通道进行监听
            cf.channel().closeFuture().sync();
        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
